// pages/challenge/index.js
const Page = require('../../../utils/ald-stat.js').Page;
const app = getApp();
let interstitialAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer: '',
    hrStr: 11,
    minStr: 11,
    secStr: 11,
    percent: '',
    left: 0,
    challengeRule: true,
    today: '',
    tomorrow: '',
    yesterday: '',
    freechou: true,
    norelibi: true,
    trysuc: true,
    clickedsuc: true,
    challenge: true,
    timeline: '',
    currency: '',
    step: '',
    ad_data: '',
    luck: true,
    tryfail: true,
    clickedfail: true,
    challengeclicked: true,
    erweima: '',
    ex_id: '',
    flow: '',
    seat:'',
    bonus:'',
    fufei:'',
    form_id: '',
    home_content:true,
    number:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that=this;
    if (options.home == 1) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        type: 23
      }
      var signs = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url+'/redpacket/Theme/activity_reward',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 23,
          sign: signs
        },
        method: 'POST',
        header: {
          'Cache-Control': 'max-age=60,public', //60秒
        },
        success: function (res) {
          if (res.data.code === 200) {
            that.setData({
              trysuc: false,
              clickedsuc: true,
              number: res.data.tc_reward,
              home_content:false,
            })
          }
        }
      })
    }
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-67bb3f52cef8b5aa'
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    var object = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/Challenge/Challenge/apply_judge',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.code === 300) {
          that.setData({
            ad_data: false
          })
        } else {
          that.setData({
            ad_data: res.data.data
          })
        }
      }
    })
    if (that.data.erweima == 3) {
      if(that.data.seat==10){
        var challenge_id = that.data.tomorrow.id;
        var form_id = that.data.form_id;
        var object = {
          unionid: wx.getStorageSync('openid'),
          challenge_id: challenge_id,
          openid: wx.getStorageSync('open_id'),
          form_id: form_id,
          type: 2,
        }
        var signs = app.sort_ASCII(object)
        wx.request({
          url: app.globalData.new_url + '/Challenge/Challenge/apply',
          data: {
            unionid: wx.getStorageSync('openid'),
            challenge_id: challenge_id,
            openid: wx.getStorageSync('open_id'),
            form_id: form_id,
            type: 2,
            sign: signs
          },
          method: 'POST',
          success: function (res) {
            console.log(res)
            that.setData({
              challengeclicked: true,
              challenge: false,
            })
          }
        })
      }else if (that.data.seat == 12) {
        var challenge_id = that.data.tomorrow.id;
        var object = {
          unionid: wx.getStorageSync('openid'),
          challenge_id: challenge_id,
        }
        var signs = app.sort_ASCII(object)
        wx.request({
          url: app.globalData.new_url + '/Challenge/Challenge/get_extra_bonus',
          data: {
            unionid: wx.getStorageSync('openid'),
            challenge_id: challenge_id,
            sign: signs
          },
          method: 'POST',
          success: function (res) {
            console.log(res)
            that.setData({
              challenge: false,
              challengeclicked:true,
            })
          }
        })
      }
      that.data.erweima = 0
    } else if (that.data.erweima == 4) {
      that.setData({
        clickedfail: true,
        tryfail: false,
      })
      that.data.erweima = 0
    }
    if (app.globalData.scene === 1038 && that.data.flow) {
      var ex_id = that.data.ex_id
      var ad_id = that.data.ad_data.ad_id;
      var object = {
        unionid: wx.getStorageSync('openid'),
        ad_id: ad_id,
        ex_id: ex_id,
        type: 2
      }
      var signs = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/Challenge/Challenge/adv_back',
        data: {
          unionid: wx.getStorageSync('openid'),
          ad_id: ad_id,
          ex_id: ex_id,
          type: 2,
          sign: signs
        },
        method: 'POST',
        success: function(res) {
          that.data.flow=false;
          console.log(res.data.msg, res)
          if (res.data.code === 200) {
            if (that.data.seat == 10) {
              var challenge_id = that.data.tomorrow.id;
              var form_id = that.data.form_id;
              var object = {
                unionid: wx.getStorageSync('openid'),
                challenge_id: challenge_id,
                openid: wx.getStorageSync('open_id'),
                form_id: form_id,
                type: 2,
              }
              var signs = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/Challenge/Challenge/apply',
                data: {
                  unionid: wx.getStorageSync('openid'),
                  challenge_id: challenge_id,
                  openid: wx.getStorageSync('open_id'),
                  form_id: form_id,
                  type: 2,
                  sign: signs
                },
                method: 'POST',
                success: function (res) {
                  console.log('报名成功',res)
                  that.onShow();
                  that.setData({
                    challengeclicked: true,
                    challenge: false,
                  })
                }
              })
            }else if(that.data.seat==12){
              var challenge_id = that.data.tomorrow.id;
              var object = {
                unionid: wx.getStorageSync('openid'),
                challenge_id: challenge_id,
              }
              var signs = app.sort_ASCII(object)
              wx.request({
                url: app.globalData.new_url + '/Challenge/Challenge/get_extra_bonus',
                data: {
                  unionid: wx.getStorageSync('openid'),
                  challenge_id: challenge_id,
                  sign: signs
                },
                method: 'POST',
                success: function (res) {
                  console.log(res)
                  that.setData({
                    challenge: false,
                    challengeclicked: true,
                  })
                }
              })
            }
          }else{
            that.setData({
              tryfail:false,
              clickedfail:true,
            })
          }
        }
      })
    }
    wx.request({
      url: app.globalData.new_url + '/Challenge/Challenge/act_list',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        if (res.data.data.now.is_apply==1){
          if(res.data.data.step>=3000){
            that.setData({
              percent:100
            })
          } else if (res.data.data.step >= 2000){
            that.setData({
              percent:66
            })
          } else if (res.data.data.step >= 1000){
            that.setData({
              percent: 33
            })
          }
        }
        that.setData({
          today: res.data.data.now,
          tomorrow: res.data.data.next,
          yesterday: res.data.data.prev,
          currency: res.data.data.currency,
        }, () => {
          that.nowTime();
        })
      }
    })
  },

  onPullDownRefresh: function () {
    let that = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    that.onShow(); // 刷新页面
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  nowTime: function() {
    const that = this;
    var second = Number(that.data.tomorrow.apply_out_time);
    const tim = setInterval(function() {
      second--;
      var hr = Math.floor(second / 3600);
      var hrStr = hr.toString();
      if (hrStr.length == 1) hrStr = '0' + hrStr;
      // 分钟位  
      var min = Math.floor(second / 60 % 60);
      var minStr = min.toString();
      if (minStr.length == 1) minStr = '0' + minStr;
      // 秒位  
      var sec = Math.floor(second % 60);
      var secStr = sec.toString();
      if (secStr.length == 1) secStr = '0' + secStr;
      var timeline = hrStr + ":" + minStr + ":" + secStr;
      that.setData({
        hrStr: hrStr,
        minStr: minStr,
        secStr: secStr,
        timeline: timeline
      })
      if(second<=0){
        clearInterval(that.data.timer);
        that.onShow();
      }
    }, 1000)
    that.setData({
      timer: tim
    })
  },
  signUps: function(e) {
    const that = this;
    var form_id = e.detail.formId;
    that.data.form_id = form_id
    var challenge_id = that.data.tomorrow.id;
    var object = {
      unionid: wx.getStorageSync('openid'),
      challenge_id: challenge_id,
      openid: wx.getStorageSync('open_id'),
      form_id: form_id,
      type: 1,
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/Challenge/Challenge/apply',
      data: {
        unionid: wx.getStorageSync('openid'),
        challenge_id: challenge_id,
        openid: wx.getStorageSync('open_id'),
        form_id: form_id,
        type: 1,
        sign: sign
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        if(res.data.code===200){
          that.setData({
            challengeclicked: true,
            challenge: false,
            freechou: true,
            fufei: 1
          })
          that.onShow();
        }
      }
    })
  },
  signUp: function(e) {
    const that = this;
    var form_id = e.detail.formId;
    that.data.form_id = form_id
    var currency = Number(that.data.currency);
    if (that.data.ad_data) {
      that.setData({
        freechou: false,
      })
    } else if (currency > 3) {
      wx.showModal({
        title: '提示',
        content: '确认消耗3个热力币发起挑战吗？',
        success(res) {
          if (res.confirm) {
            var challenge_id = that.data.tomorrow.id;
            var object = {
              unionid: wx.getStorageSync('openid'),
              challenge_id: challenge_id,
              openid: wx.getStorageSync('open_id'),
              form_id: form_id,
              type: 1,
            }
            var sign = app.sort_ASCII(object)
            wx.request({
              url: app.globalData.new_url + '/Challenge/Challenge/apply',
              data: {
                unionid: wx.getStorageSync('openid'),
                challenge_id: challenge_id,
                openid: wx.getStorageSync('open_id'),
                form_id: form_id,
                type: 1,
                sign: sign
              },
              method: 'POST',
              success: function (res) {
                console.log(res)
                if (res.data.code === 200) {
                  that.setData({
                    challengeclicked: true,
                    challenge: false,
                    fufei: 1
                  })
                  that.onShow();
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else {
      that.setData({
        norelibi: false,
      })
    }
  },
  goindex: function() {
    let pages = getCurrentPages();
    let prevPage = pages[0]
    if (prevPage.route == "pages/tarbar/index/index") {
      prevPage.setData({
        isShowInterstitialAd: 100,
      })
    }
    wx.switchTab({
      url: '/pages/tarbar/index/index',
    })
  },
  gomoney: function() {
    wx.navigateTo({
      url: '../../other/moneyDetail/index',
    })
  },
  navigateToAds: function (e) {
    const that = this;
    const ad_data = that.data.ad_data;
    const ad_id = ad_data.ad_id;
    if (ad_data.way == 1) {
      wx.navigateToMiniProgram({
        appId: ad_data.appid,
        path: ad_data.path,
        success(res) {
          var object = {
            unionid: wx.getStorageSync('openid'),
            ad_id: ad_id,
          }
          var sign = app.sort_ASCII(object)
          wx.request({
            url: app.globalData.new_url + '/Challenge/Challenge/adv_back',
            data: {
              unionid: wx.getStorageSync('openid'),
              ad_id: ad_id,
              type: 1,
              sign: sign
            },
            method: 'POST',
            success: function (res) {
              console.log(res.data.msg, res)
              that.setData({
                challenge: true,
                challengeclicked: false,
                trysuc: true,
                clickedsuc: false,
                luck: true,
              })
              that.data.ex_id = res.data.ex_id
              that.data.flow = true
            }
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=8',
      })
      that.setData({
        challenge: true,
        challengeclicked: false,
        trysuc: true,
        clickedsuc: false,
        luck: true
      })
    }
  },
  navigateToAd: function(e) {
    const that = this;
    console.log(e.target.dataset.seat)
    that.data.seat = e.target.dataset.seat;
    const ad_data = that.data.ad_data;
    const ad_id = ad_data.ad_id;
    if (ad_data.way == 1) {
      wx.navigateToMiniProgram({
        appId: ad_data.appid,
        path: ad_data.path,
        success(res) {
          var object = {
            unionid: wx.getStorageSync('openid'),
            ad_id: ad_id,
          }
          var sign = app.sort_ASCII(object)
          wx.request({
            url: app.globalData.new_url + '/Challenge/Challenge/adv_back',
            data: {
              unionid: wx.getStorageSync('openid'),
              ad_id: ad_id,
              type: 1,
              sign: sign
            },
            method: 'POST',
            success: function(res) {
              console.log(res.data.msg, res)
              that.setData({
                challenge: true,
                challengeclicked: false,
                trysuc: true,
                clickedsuc: false,
                luck:true,
              })
              that.data.ex_id = res.data.ex_id
              that.data.flow = true
            }
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=8',
      })
      that.setData({
        challenge: true,
        challengeclicked: false,
        trysuc: true,
        clickedsuc: false,
        luck: true
      })
    }
  },
  getchance: function(e) {
    const that = this;
    that.data.seat = e.target.dataset.seat;
    if (that.data.ad_data.way == 0) {
      var ad_id = that.data.ad_data.ad_id
      wx.navigateTo({
        url: '../../other/erweima/index?ad_id=' + ad_id + '&&plate=8',
      })
      that.setData({
        freechou: true,
      })
    } else {
      that.setData({
        freechou: true,
        luck: false,
      })
    }
  },
  lingjiang:function(){
    const that=this;
    var challenge_id=that.data.yesterday.id;
    var object = {
      unionid: wx.getStorageSync('openid'),
      challenge_id: challenge_id
    }
    var sign = app.sort_ASCII(object)
    wx.request({
      url: app.globalData.new_url + '/Challenge/Challenge/receive_bonus',
      data: {
        unionid: wx.getStorageSync('openid'),
        challenge_id: challenge_id,
        sign: sign
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if(res.data.code===300){
          wx.showModal({
            title: '提示',
            content: '暂未开奖，请在8：00之后领取',
            showCancel:false
          })
        }else{
          that.setData({
            bonus: res.data.bonus,
            clickedsuc: true,
            trysuc: false,
            home_content:true,
          })
        }
      }
    })
  },
  continueplay: function() {
    const that = this;
    if (that.data.seat == 12) {
      that.setData({
        challenge: false,
        challengeclicked:true,
        tryfail: true,
        clickedfail: false
      })
    } else if(that.data.seat==10){
      that.setData({
        freechou: false,
        tryfail: true,
        clickedfail: false
      })
    }
  },
  onHide: function() {
    clearInterval(this.data.timer);
    app.globalData.scene = 1001;
  },
  onUnload: function() {
    clearInterval(this.data.timer);
  },
  hideHandles:function(){
    const that=this;
    that.setData({
      trysuc: true,
      clickedsuc: false,
    })
    that.onShow();
  },
  hideHandleos:function(){
    const that=this;
    that.setData({
      challenge: true,
      challengeclicked: false,
    },()=>{
      if (interstitialAd) {
        interstitialAd.show().catch((err) => {
          console.error(err)
        })
      }
    })
  },
  hideHandle: function() {
    this.setData({

      challengeRule: true,
      freechou: true,
      norelibi: true,
      tryfail:true,
      clickedfail:false,
      luck:true,
    })
  },
  challengeRule: function() {
    this.setData({
      challengeRule: false
    })
  }
})